using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class FreeUsePanel : MonoBehaviour
{
    private Transform _panel;
    private Button _button;
    private Image _content;
    private Action<int> _action;
    private int _index;

    public void Init()
    {
        _panel = transform.Find("Panel");
        _content = transform.Find("Panel/Content").GetComponent<Image>();
        _button = transform.Find("Panel/Button").GetComponent<Button>();

        _button.onClick.AddListener(OnButtonClick);
        transform.Find("Mask").GetComponent<Button>().onClick.AddListener(() => Destroy(this.gameObject));
    }

    public static void Show(int index, Action<int> action = null)
    {
        GameObject go = GameManager.Instance.LoadPrefab(Constants.PathFreeUsePanel);
        go.transform.SetParent(GameObject.Find("Canvas").transform);
        go.transform.localPosition = Vector3.zero;
        RectTransform rectTransform = go.GetComponent<RectTransform>();
        rectTransform.anchorMin = Vector2.zero;
        rectTransform.anchorMax = Vector2.one;
        rectTransform.sizeDelta = Vector2.zero;
        rectTransform.localScale = Vector3.one;

        var panel = go.GetComponent<FreeUsePanel>();
        panel.Init();
        panel.ShowPanel(index, action);
    }

    public void ShowPanel(int index, Action<int> action = null)
    {
        _index = index;
        _panel.transform.localScale = Vector3.zero;
        _panel.transform.DOScale(Vector3.one, 0.3f).SetEase(Ease.OutBounce);
        Sprite sp = GameManager.Instance.LoadSprite($"{Constants.PathWaterGunSprite}_{index}", true);
        _content.sprite = sp;
        _action = action;

        AdManager.ShowNative();
    }

    private void OnButtonClick()
    {
        GameManager.Instance.PlayBtnClickAudio();
        AdManager.ShowVideo(() => { _action?.Invoke(_index); });
        Destroy(this.gameObject);
    }
}